package sysAuth

import initConfig.SysInitData
import model.SelectData
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysAuthSQL
import sysUtil.SysCommUtil
import xdInterface.XdControllerInterface

class SysParamsController implements XdControllerInterface{


    def authUtilsService
    def sysDBUtilService

    def index() {query()}

    @Override
    def delete() {
        del()
    }

    @Override
    def detail() {
        return null
    }

    @Override
    def del() {
        def result = ResultJson.resultJson()
        try {
            def user = SysUsers.findByUserNo(params.userNo)
            def id = params?.ID
            def obj = SysParams.get(Integer.parseInt(id))
            if(obj){
                if(obj.creator != params.userNo && !SysInitData.USER_GRADE_ADMIN_FUNC.contains(authUtilsService.isAvailable(user)) ){
                    result.put(ResultJson.CODE.name,ResultCode.OPT_NOT_AUTH.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_NOT_AUTH.message)
                }else{
                    obj.delete(flush: true)
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                }
            }else {
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }

        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            //println result.toString()
            render result.toString()
        }
    }

    @Override
    def edit() {
        return null
    }

    @Override
    def save() {
        return null
    }

    @Override
    def update() {
        def result = ResultJson.resultJson()
        try {
            def id = params?.ID
            def obj = SysParams.get(Integer.parseInt(id))
            if(obj){
                def old = SysParams.findByParamsTypeAndValue(obj.paramsType,params.VALUE?:null)
                println SysCommUtil.copyMap(old)
                println SysCommUtil.copyMap(old)
                if( old && old?.id != obj?.id){
                    result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                    result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
                }else {
                    obj.setLabel(params.LABEL?:null)
                    obj.setOrderNum(Integer.parseInt(params.ORDER_NUM?:"0"))
                    obj.setState(params.STATE?:"0")
                    obj.setParamsDesc(params.PARAMS_DESC?:"")
                    obj.setValue(params.VALUE?:null)
                    obj.setUpdater(params?.updater)
                    obj.save(flush: true)
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                }

            }else {
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }

        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            //println result.toString()
            render result.toString()
        }
    }

    @Override
    def add() {
        def result = ResultJson.resultJson()
        try {
            def id = params?.ID
            def obj = SysParams.findByParamsTypeAndValue(params.get("PARAMS_TYPE"),params.get("VALUE"))
            if(obj){
                result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
            }else {
                obj = new SysParams()
                obj.setLabel(params.get("LABEL"))
                obj.setOrderNum(Integer.parseInt(params.get("ORDER_NUM")?:"0"))
                obj.setState(params.get("STATE"))
                obj.setParamsDesc(params.get("PARAMS_DESC"))
                obj.setValue(params.get("VALUE"))
                obj.setParamsType(params.get("PARAMS_TYPE"))
                obj.setCreator(params.userNo)
                obj.save(flush: true)
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            //println result.toString()
            render result.toString()
        }
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def query() {
        def result = ResultJson.resultJson()
        try {

            def paramsType = params?.paramsType
            def keyword = params?.keyword
            def offset = Integer.parseInt(String.valueOf(params.page?:"1"))
            def max = Integer.parseInt(String.valueOf(params.limit?:"10"))
            def state = params?.state
            def sql = SysAuthSQL.getParams(paramsType,keyword,state,null)


            def list =   sysDBUtilService.pageResult(sql,offset,max, sysDBUtilService.getGroovySqlInstance())
            def count =  sysDBUtilService.pageCount(sql, sysDBUtilService.getGroovySqlInstance())

            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.DATA.name,list )
            result.put(ResultJson.COUNT.name,count )
            result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            //println result.toString()
            render result.toString()
        }
    }

    @Override
    def getOne() {
        def result = ResultJson.resultJson()
        try {

            def id = params?.id

            def sql = SysAuthSQL.getParams(null,null,null,id)
            def list =   sysDBUtilService.pageResult(sql,1,1, sysDBUtilService.getGroovySqlInstance())

            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.DATA.name,list )
            result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            //println result.toString()
            render result.toString()
        }
    }

    /**
     * params  下拉框
     * @return
     */
    def getParamSelect(){
        def result = ResultJson.resultJson()
        try {
            def paramsType = params?.paramsType
            def keyword = params?.keyword
            def offset = Integer.parseInt(String.valueOf(params.page?:"1"))
            def max = Integer.parseInt(String.valueOf(params.limit?:"10"))
            def state = params?.state
            def sql = SysAuthSQL.getParams(paramsType,keyword,state,null)
            def list =   sysDBUtilService.pageResult(sql,offset,max, sysDBUtilService.getGroovySqlInstance())

            def selectList = []
            list.each {
                def select =new SelectData( name: it.LABEL,value: it.VALUE,disabled: it.state=="0"?"disabled":"")
                selectList.add( SysCommUtil.domainToMap(select) )
            }

            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.DATA.name,selectList )
            result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            //println result.toString()
            render result.toString()
        }
    }
}
